package gtgold;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.event.*;
import java.io.*;

public class MenuBar
{
	GameData data;
	JMenuBar menuBar;
	
	public MenuBar()
	{
		JMenu file, about;
		JMenuItem newGame, saveGame, loadGame, exit;
		JMenuItem _about;
		
		menuBar = new JMenuBar();
		
		file = new JMenu("File");
		about = new JMenu("About");
		
		newGame = new JMenuItem("New Game");
		saveGame = new JMenuItem("Save Game");
		loadGame = new JMenuItem("Load Game");
		exit = new JMenuItem("Exit Game");
		_about = new JMenuItem("About");
		
		newGame.setAccelerator(KeyStroke.getKeyStroke("control N"));
		saveGame.setAccelerator(KeyStroke.getKeyStroke("control S"));
		loadGame.setAccelerator(KeyStroke.getKeyStroke("control O"));
		exit.setAccelerator(KeyStroke.getKeyStroke("control X"));
		
		newGame.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (JOptionPane.showConfirmDialog(OregonTrail.frame, "Are you sure? Unsaved progress will be lost!",
						"New Game", JOptionPane.YES_NO_OPTION) == 0)
				{
					OregonTrail.reset();
					data = null;
				}
			}
		});
		
		saveGame.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (data != null)
				{
					JFileChooser dialog = new JFileChooser();
					
					FileNameExtensionFilter filter = new FileNameExtensionFilter(
					        "GT Gold Game Saves", "gtg");
					dialog.setFileFilter(filter);
					
					dialog.showSaveDialog(OregonTrail.frame);
					
					try
					{
						String file = dialog.getSelectedFile().getCanonicalPath();
						if (!file.substring(file.length()-3).equals("gtg"))
							file += ".gtg";
						
						FileOutputStream fos = new FileOutputStream(new File(file));
						ObjectOutputStream oos = new ObjectOutputStream(fos);
						oos.writeObject(data);
						oos.close();
						
						JOptionPane.showMessageDialog(OregonTrail.frame, "Game saved.", "Success",
								JOptionPane.INFORMATION_MESSAGE);
					}
					catch (IOException ioe)
					{
						JOptionPane.showMessageDialog(OregonTrail.frame, "You royally screwed up, n00b", "Error",
								JOptionPane.ERROR_MESSAGE);
					}
					catch (NullPointerException npe)
					{
					}
				}
				else
					JOptionPane.showMessageDialog(OregonTrail.frame, "Nothing to save", "Error",
							JOptionPane.ERROR_MESSAGE);
			}
		});
		
		loadGame.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JFileChooser dialog = new JFileChooser();
				
				FileNameExtensionFilter filter = new FileNameExtensionFilter(
				        "GT Gold Game Saves", "gtg");
				dialog.setFileFilter(filter);
				
				dialog.showOpenDialog(OregonTrail.frame);
				
				try
				{
					String file = dialog.getSelectedFile().getCanonicalPath();
					if (!file.substring(file.length()-3).equals("gtg"))
					{
						JOptionPane.showMessageDialog(OregonTrail.frame, "Unrecognized file format", "Error",
								JOptionPane.ERROR_MESSAGE);
						return;
					}
					
					
					FileInputStream fis = new FileInputStream(dialog.getSelectedFile());
					ObjectInputStream ois = new ObjectInputStream(fis);
					
					data = (GameData)ois.readObject();
					
					JOptionPane.showMessageDialog(OregonTrail.frame, "Game loaded successfully!", "Success",
							JOptionPane.INFORMATION_MESSAGE);
					
					// begin the loaded game
					@SuppressWarnings("unused")
					GameManager gm = new GameManager(data);
				}
				catch (IOException ioe)
				{
					JOptionPane.showMessageDialog(OregonTrail.frame, "You royally screwed up, n00b", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
				catch (ClassNotFoundException cnfe)
				{
					JOptionPane.showMessageDialog(OregonTrail.frame, "You royally screwed up, n00b", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
				catch (NullPointerException npe)
				{
				}
			}
		});
		
		exit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (JOptionPane.showConfirmDialog(OregonTrail.frame, "Are you sure? Unsaved progress will be lost!",
						"Exit Game", JOptionPane.YES_NO_OPTION) == 0)
					OregonTrail.frame.dispose();
			}
		});
		
		_about.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				StringBuilder text = new StringBuilder();
				text.append("Oregon Trail by the GT Gold Team\nCS 2340 Project\nGeorgia Institute of Technology\n\n");
				text.append("Jo Kim - Programmer\n");
				text.append("Joe Oh - Programmer\n");
				text.append("Robert Grosse - Programmer\n");
				text.append("Isabelle Giuong - Graphic Artist\n");
				text.append("Jen Paek - Graphic Artist\n");
				
				JOptionPane.showMessageDialog(OregonTrail.frame, text, "About",
						JOptionPane.INFORMATION_MESSAGE);
			}
		});
		
		file.add(newGame);
		file.add(new JSeparator());
		file.add(saveGame);
		file.add(loadGame);
		file.add(new JSeparator());
		file.add(exit);
		
		about.add(_about);
		
		menuBar.add(file);
		menuBar.add(about);
	}
	
	
}
